﻿#ifndef MODBUSSENSOR_H
#define MODBUSSENSOR_H
#include <QObject>
#include <QSerialPort>
#include <QMap>
class ModbusSensor : public QObject
{
    Q_OBJECT
public:
    explicit ModbusSensor(QObject *parent = nullptr);
    ~ModbusSensor();
    bool open(const QString &portName);
    void close();
    // 传感器数据读取
    QMap<QString, double> readAllSensors(quint8 address = 0xFF);
    // 设备地址管理
    quint8 readDeviceAddress();
    bool changeDeviceAddress(quint8 newAddress);
private:
    QSerialPort *serialPort;

    quint16 calculateCRC(const QByteArray &data);
    QByteArray sendCommand(const QByteArray &command, int expectedLength, int timeout = 200);
};
#endif // MODBUSSENSOR_H
